﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Timer = System.Windows.Forms.Timer;

namespace Launcher
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void SetMsg(string msg)
        {
            this.Invoke(new Action(() => Message.Text = msg));
        }

        void Start()
        {
            try
            {
                SetMsg("Запуск...");
                SoftWare s = new SoftWare() { Productname = "VMDS" };
                s.Update();
                Process.Start(s.InstallPath+"\\VMDS.GUIclient.exe");
            }
            catch (Exception e)
            {
                
                SetMsg("Не удалось запустить программу");
                CloseButton.Invoke(new Action(() =>
                                                  {
                                                      MessageBox.Show(e.ToString());
                                                      CloseButton.Visible = true;
                                                  }));
            }
        }

     

      public void CalkArgs(string[] args)
      {
          if (args != null)
          {
              if (args.Contains("start")) DisplayStateHolder.State = DisplayState.Starting;
              if (args.Contains("update")) DisplayStateHolder.State = DisplayState.Updating;
          }
      }

      void SetOpacity()
      {
          var j = 0.01;
          for (int i = 0; i < 100; i++)
          {
              Thread.Sleep(100);
              try
              {
                  j += .1;
                  if (j > 1) break;
                  this.Invoke(new Action(() => { this.Opacity = j; }));
              }
              catch { }
          }
      }

        private Timer t;
        private bool off;
        private bool on;
        private string old;
        private SoftWare s;
        void Update()

        {
            off = false;
            on = false;
            s=new SoftWare(){Productname = "VMDS"};
            s.Update();
            old = s.ProductVersion;
            SetMsg("Обновление...");
            while (s.ProductVersion==old)
            {
                s.Update();
            }
            
            Start();
            
        }

    
        void Loading()
        {
           SetOpacity();
            if (DisplayStateHolder.State==DisplayState.Starting)Start();
            if (DisplayStateHolder.State==DisplayState.Updating)Update();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Text = "VmdsLauncher";
            string[] args = Environment.GetCommandLineArgs();
            CalkArgs(args);
            Thread t = new Thread(Loading);
            t.Start();
            
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
    }
}
